<template>
  <div :id="id" ref="boxwrap" :class="className" :style="{height:height,width:width}" :title="title" :data="data" />
</template>

<script>
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: ''
    },
    data: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      chart: null,
      fontColor: '#bebebe',
      color: ['deepskyblue', 'orange', 'purple']
    }
  },
  watch: {
    data: {
      immediate: true,
      handler: function(newval, oldval) {
        this.data = newval
        if (this.chart) {
          this.chart.dispose()
          this.chart = null
          this.initChart()
        }
      },
      deep: true
    }
  },
  mounted() {
    this.domResize()
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const series = []
      for (const k in this.data) {
        series.push({
          name: k,
          type: 'line',
          smooth: true,
          label: {
            show: true,
            position: 'top',
            color: this.fontColor,
            formatter: (param) => {
              return param.value[1] + '%'
            }
          },
          data: this.data[k]
        })
      }
      this.chart = this.$echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        color: this.color,
        title: {
          text: this.title,
          top: '1%',
          left: '5%',
          textStyle: {
            fontSize: 16,
            color: this.fontColor
          }
        },
        tooltip: {
          trigger: 'axis',
          formatter: (param) => {
            let str = ''
            param.forEach(item => {
              str += `${item.marker}${item.seriesName}&nbsp;&nbsp;${item.value[0]}&nbsp;${item.value[1]}% <br>`
            })
            return str
          }
        },
        legend: {
          orient: 'vertical',
          top: '3%',
          right: '1%',
          textStyle: {
            fontSize: 14,
            color: this.fontColor
          }
        },
        grid: {
          top: '20%',
          left: '5%',
          right: '20%',
          bottom: '15%',
          containLabel: true
        },
        dataZoom: [{
          show: true,
          type: 'inside',
          bottom: 10,
          height: 10,
          star: 50,
          end: 100
        }, {
          show: true,
          bottom: 10,
          height: 10,
          start: 50,
          end: 100
        }],
        xAxis: [{
          type: 'time',
          boundaryGap: false,
          minInterval: 3600 * 24 * 1000,
          axisLabel: {
            color: this.fontColor,
            formatter: (value) => {
              const date = new Date(value)
              return (date.getMonth() + 1) + '/' + date.getDate()
            }
          }
        }],
        yAxis: [{
          type: 'value',
          name: '%',
          nameTextStyle: {
            color: this.fontColor
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: this.fontColor
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.1)'
            }
          }
        }],
        series: series
      })
    },
    domResize() {
      const that = this
      const dom = this.$refs.boxwrap
      this.$erd.listenTo(dom, function(element) {
        that.chart.resize()
      })
    }
  }
}
</script>
